package org.example.managementsystem.repository;

import org.example.managementsystem.model.Product_Plan_Detail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Objects;
@Repository
public class Product_Plan_DetailDao {
    private final JdbcTemplate jdbcTemplate;
    @Autowired
    public Product_Plan_DetailDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    //添加计划
    public Long insertProduct_Plan_Detail(Product_Plan_Detail product_plan_detail) {
        String sql = "INSERT INTO production_plan_detail ( plan_id, product_code, product_quantity) " +
                "VALUES (?, ?, ?)";

        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(connection -> {
            PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

            ps.setLong(1,product_plan_detail.getPlan_id());
            ps.setString(2,product_plan_detail.getProduct_code());
            ps.setInt(3,product_plan_detail.getProduct_quantity());
            return ps;
        }, keyHolder);
        return Objects.requireNonNull(keyHolder.getKey()).longValue();
    }
    public int updateProduct_Plan_Detail(Product_Plan_Detail product_plan_detail) {
        String sql = "UPDATE production_plan_detail SET  plan_id = ?, product_code = ?, product_quantity = ?" +
                "WHERE detail_id = ?";

        return jdbcTemplate.update(sql,
                product_plan_detail.getPlan_id(),
                product_plan_detail.getProduct_code(),
                product_plan_detail.getProduct_quantity(),
                product_plan_detail.getDetail_id());
    }
    public Product_Plan_Detail getProduct_Plan_DetailById(Long detail_id) {
        String sql = "SELECT * FROM production_plan_detail WHERE detail_id = ?";
        List<Product_Plan_Detail> product_plan_details = jdbcTemplate.query(sql, new Product_Plan_DetailRowMapper(), detail_id);
        return product_plan_details.isEmpty() ? null : product_plan_details.get(0);
    }
    public int deleteProduct_Plan_Detail(Long detail_id) {
        String sql = "DELETE FROM production_plan_detail WHERE detail_id = ?";
        return jdbcTemplate.update(sql, detail_id);
    }
    // 根据plan_id查找对应的数据
    public List<Product_Plan_Detail> getProduct_Plan_DetailsByPlanId(Long plan_id) {
        String sql = "SELECT * FROM production_plan_detail WHERE plan_id = ?";
        return jdbcTemplate.query(sql, new Product_Plan_DetailRowMapper(), plan_id);
    }

    public List<Product_Plan_Detail> getAllProduct_Plan_Details() {
        String sql = "SELECT * FROM production_plan_detail";
        return jdbcTemplate.query(sql, new Product_Plan_DetailRowMapper());
    }
    // Product_Plan_DetailDao.java
    public int updateProduct_Plan_DetailById(Product_Plan_Detail detail) {
        String sql = "UPDATE production_plan_detail SET plan_id = ?, product_code = ?, product_quantity = ? " +
                "WHERE detail_id = ?";
        return jdbcTemplate.update(sql, detail.getPlan_id(), detail.getProduct_code(),
                detail.getProduct_quantity(), detail.getDetail_id());
    }
    private static class Product_Plan_DetailRowMapper implements RowMapper<Product_Plan_Detail> {
        @Override
        public Product_Plan_Detail mapRow(ResultSet rs, int rowNum) throws SQLException {
            Product_Plan_Detail product_plan_detail = new Product_Plan_Detail();
            product_plan_detail.setPlan_id(rs.getLong("plan_id"));
            product_plan_detail.setDetail_id(rs.getLong("detail_id"));
            product_plan_detail.setProduct_code(rs.getString("product_code"));
            product_plan_detail.setProduct_quantity(rs.getInt("product_quantity"));

            return product_plan_detail;
        }
    }
}
